<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PSAdmin.Web.Models.PSScriptEditViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Edit Script
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="span-12">
        <h2>Edit Script</h2>
    </div>
    <div class="span-12 last right">
        <%using (Html.BeginForm("Delete", "Script", new { id = Model.Script.Id })) { %>
            <input type="submit" value="Delete" id="deleteScript" />
        <%} %>
    </div>
    <div class="clear"></div>
    
    <%=Html.ValidationSummary() %>

    <% using (Html.BeginForm()) {%>
    
    <fieldset>
        <legend>Fields</legend>
        <%Html.RenderPartial("ScriptForm", Model.Script); %>      
        
        <p>
            <label for="scriptRoles">Security Roles</label>
        </p>
        <div class="scriptRoles">
            <%=Html.CheckBoxList<Guid>("scriptRoles", Model.AvailableRoles, Model.SelectedRoles) %>
        </div>
        <p><input type="submit" value="Save" /></p>
    </fieldset>
    <%} %>
    
    <%if (Model.Script.HasVariables) { %>
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Description</th>
                <th>Token</th>
                <th></th>
                <th></th>
            </tr>
            <%foreach (var v in Model.Script.Variables)
              { %>
            
                <tr>
                    <td><%=Html.Encode(v.Name)%></td>
                    <td><%=Html.Encode(v.Description)%></td>
                    <td><%=Html.Encode(v.Token)%></td>
                    <td><%=Ajax.ActionLink("Edit", "GetVariable", "Script", new { id = v.Id, scriptId = Model.Script.Id },
                            new AjaxOptions { UpdateTargetId = "variabledetails" })%></td>
                    <td><a href="<%=Url.Action("DeleteVariable", new { id = v.Id, scriptId = Model.Script.Id }) %>" onclick="DeleteVariable(this);return false;">Delete</a></td>
                </tr>
            
            <% } %>
                      
        </thead>
    </table>
    <%} %>   
    <div id="variabledetails">
    
    </div>
  
    <% using (Html.BeginForm("CreateVariable", "Script", new { scriptId = Model.Script.Id })) {%>
    <fieldset>
        <legend>Create new variable</legend>
        <p>
            <label for="NewVariableName">Name</label>               
            <input type="text" id="NewVariableName" name="NewVariableName" />
        </p>
        <p>
            <label for="NewVariableDescription">Description</label>               
            <input type="text" id="NewVariableDescription" name="NewVariableDescription" />
        </p>
        <p>
            <label for="NewVariableToken">Token</label>               
            <input type="text" id="NewVariableToken" name="NewVariableToken" />
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
    <% } %>

    <div>
        <%=Html.ActionLink("Back to List", "List") %>
    </div>
    
    
    <script type="text/javascript">

        $(document).ready(function() {
            $("#deleteScript").click(function() {
                var response = confirm("Are you sure you want to delete this script?");
                if (response == false)
                    return false;
            });
        });
        
        function DeleteVariable(link) {

            var url = $(link).attr('href');

            $.post(url, null, function(data) {
                if (data == true) {
                    $(link).parents('tr').remove();
                    $("#variabledetails").html('');
                }
            }, "json");
        }
    </script>
    
</asp:Content>

